layui.use(['admin', 'form', 'xmSelect'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
        , xmSelect = layui.xmSelect
        , form = layui.form;

    let layerIndex = '';
    layui.data.sendParams = function (params) {
        layerIndex = params.lay_index;
    }

    let field = {
        isTree: false,
        absoluteAuth: true, //
        initKey: '', // 初始key名称
        initValue: '', // 初始值
        select: true,
        quick_search: '',
        action: "index"
    };
    // 获取菜单列表
    admin.req({
        url: setter.request.domainName + '/admin/auth.menu/index'
        , data: field
        , type: 'get'
        , loading: true // 是否开启加载框
        , done: function (res) {
            let selectData = [];
            let initData   = [];
            if(res.data.options){
                selectData = res.data.options;
                let pid = $("#pid").val();
                if(pid !== 0 && pid !== '0' && pid){
                    initData = [pid];
                }
            }
            // 下拉选择框
            xmSelect.render({
                el: '#menuXmSelect',
                prop: {
                    name: 'title',
                    value: 'id',
                },
                language: 'zn',
                autoRow: true, // 自动换行
                filterable: true, // 开启搜索模式
                paging: true, // 开启分页
                pageSize: 50, // 每页显示条数
                initValue: initData, // 默认选中
                radio: true, // 开启单选
                clickClose: true, // 单选完关闭下拉
                disabled: true,
                toolbar: {
                    show: false, // 显示工具条
                    list: [ 'ALL', 'CLEAR', 'REVERSE' ], // 全选、清空、反选
                },
                data: selectData,
                on: function(data){
                    // change, 此次选择变化的数据,数组
                    let change = data.change;
                    $("#pid").val(change[0]['id'])
                }
            })
        }
    })

    form.on('switch(statusSwitch)', function(data){
        let status = 0;
        if(data.elem.checked){
            status = 1;
        }
        form.val("layPopup", {
            "status": status
        });
    });

    // 开启提交
    form.on('submit(laySubmitEdit)', function (data) {
        let field = data.field; //获取提交的字段
        admin.req({
            url: setter.request.domainName + '/admin/auth.menu/edit'
            , data: field
            , type: 'post'
            , loading: true // 是否开启加载框
            , done: function(res){
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layui.treeTable.reload('menuTable'); //重载表格
                layer.close(layerIndex); //执行关闭
            }
        });
    });

})
